package panels;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class JFieldBox extends JPanel implements PropertyChangeListener, ActionListener {

	/**
	 *  FieldPanel
	 */
	private static final long serialVersionUID = 1L;
	
	protected static final boolean myCmp(Object A, Object B) {
		if (A==null) {
			return B==null;
		} else {
			return (B!=null) && A.equals(B);
		}
	}
	
	protected static final String	myStr(Object A) {
		if (A==null) {
			return "null";
		} else {
			return A.toString();
		}
	}
	
	public static final String	captionChanged	= "captionChanged";
	public static final String	textChanged		= "textChanged";

	protected	String			oldCaption = "";
	protected	String			oldText = "";
	protected	JLabel			captionLbl = null;
	protected	JTextField		textField = null;
	protected	BorderLayout	layout = null;
	protected	Orientation		orientation = Orientation.vertical;
	protected	boolean			showCaption = true;
				
	public enum Orientation {
		horizontal,
		vertical
	}
	
	public JFieldBox(String caption, String text) {
		layout = new BorderLayout();
		
		captionLbl = new JLabel(caption);
		captionLbl.addPropertyChangeListener(this);
		setCaption(caption);
		
		textField = new JTextField(text);
		textField.addPropertyChangeListener(this);
		textField.addActionListener(this);
		setText(text);
		
		setLayout(layout);
		captionLbl.setVisible(true);
		textField.setVisible(true);
		
		setCaptionHorizontalAlignment(JLabel.CENTER);
		setTextHorizontalAlignment(JLabel.CENTER);

		add(textField, BorderLayout.CENTER);
		
		setShowCaption(true);
		setOrientation(Orientation.vertical);
		setDoubleBuffered(true);
		setVisible(true);
		
		addPropertyChangeListener(this);
	}
	
	public boolean checkTextFormat(String S) {
		return true;
	}
	public boolean checkCaptionFormat(String S) {
		return true;
	}

	public boolean isShowCaption() {
		return showCaption;
	}

	public void setShowCaption(boolean showCaption) {
		this.showCaption = showCaption;
	}

	public Orientation getOrientation() {
		return orientation;
	}

	public void setOrientation(Orientation orientation) {
		this.orientation = orientation;
		this.remove(captionLbl);
		if (getOrientation() == Orientation.horizontal) {
			if (isShowCaption()) {
				this.add(captionLbl, BorderLayout.WEST);
			}
		} else {
			if (isShowCaption()) {
				this.add(captionLbl, BorderLayout.NORTH);
			}
		}
	}
	
	public synchronized String	getCaption() {
		return captionLbl.getText();
	}
	
	public synchronized void setCaption(String newCaption) {
		if (checkCaptionFormat(newCaption)) {
			oldCaption = getCaption();
			captionLbl.setText(newCaption);
			this.firePropertyChange(captionChanged,oldCaption,newCaption);
		}
	}
	
	public synchronized String getText() {
		return textField.getText();
	}
	
	public synchronized void setText(String text) {
		if (checkTextFormat(text)) {
			oldText = getText();
			textField.setText(text);
			firePropertyChange(textChanged, oldText, text);
		}
	}
	
	public void setCaptionHorizontalAlignment(int alignment) {
		captionLbl.setHorizontalAlignment(alignment);
	}
	
	public int getCaptionHorizontalAlignment() {
		return captionLbl.getHorizontalAlignment();
	}
	
	public void setTextHorizontalAlignment(int alignment) {
		textField.setHorizontalAlignment(alignment);
	}
	
	public int getTextHorizontalAlignment() {
		return textField.getHorizontalAlignment();
	}

	public Font getCaptionFont() {
		return captionLbl.getFont();
	}

	public void setCaptionFont(Font captionFont) {
		if (captionLbl!=null) captionLbl.setFont(captionFont);
	}

	public Font getTextFont() {
		return textField.getFont();
	}

	public void setTextFont(Font textFont) {
		if (textField!=null) {
			textField.setFont(textFont);
		}
	}
	
	@Override
	public void setFont(Font F) {
		super.setFont(F);
		setCaptionFont(F);
		setTextFont(F);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (myCmp(e.getSource(),textField)) {
			if (!myCmp(textField.getText(), oldText)) {
				if (!checkTextFormat(textField.getText())) {
					textField.setText(oldText);
				} else {
					String	remember = oldText;
					oldText = textField.getText();
					firePropertyChange(textChanged, remember, textField.getText());
				}
			}
		} else if (myCmp(e.getSource(),captionLbl)) {
			if (!myCmp(captionLbl.getText(), oldCaption)) {
				if (!checkCaptionFormat(captionLbl.getText())) {
					captionLbl.setText(oldCaption);
				} else {
					String	remember = oldCaption;
					oldCaption = captionLbl.getText();
					firePropertyChange(captionChanged, remember, captionLbl.getText());
				}
			}
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (myCmp(evt.getSource(), this)) {
			if (myCmp(evt.getPropertyName(), textChanged)) {
				onTextChanged(myStr(evt.getOldValue()), myStr(evt.getNewValue()));
			} else if (myCmp(evt.getPropertyName(), captionChanged)) {
				onCaptionChanged(myStr(evt.getOldValue()), myStr(evt.getNewValue()));
			}
		}
	}
	
	public void onCaptionChanged(String oldCaption, String newCaption) {
		
	}
	
	public void onTextChanged(String oldText, String newText) {
		
	}

}
